//
//  UIImage+ZXCaterory.m
//  MusiceFate
//
//  Created by simon on 13/1/25.
//  Copyright (c) 2013年 yinyuetai.com. All rights reserved.
//

#import "UIImage+ZXCaterory.h"

@implementation UIImage (ZXCaterory)


/*
 @brief  用color 和 size 创建 image；
 
 */
+ (UIImage *)zx_imageWithColor:(UIColor *)color andSize:(CGSize)size
{
      return [UIImage zx_imageWithColor:color andSize:size opaque:YES];;
}

+ (UIImage *)zx_imageWithColor:(UIColor *)color andSize:(CGSize)size opaque:(BOOL)opaque
{
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, opaque, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}







//在iOS7以前，获取一个UIView的快照有以下步骤： 首先创建一个UIGraphics的图像上下文，然后将视图的layer渲染到该上下文中，从而取得一个图像，最后关闭图像上下文，并将图像显示在UIImageView中。
/*
[view snapshotViewAfterScreenUpdates:NO];
这个方法制作了一个UIView的副本，如果我们希望视图在执行动画之前保存现在的外观，以备之后使用（动画中视图可能会被子视图遮盖或者发生其他一些变化）
 afterUpdates参数表示是否在所有效果应用在视图上了以后再获取快照。例如，如果该参数为NO，则立马获取该视图现在状态的快照，反之，以下代码只能得到一个空白快照：
 [view snapshotViewAfterScreenUpdates:YES];
 [view setAlpha:0.0];
 由于我们设置afterUpdates参数为YES，而视图的透明度值被设置成了0，所以方法将在该设置应用在视图上了之后才进行快照，于是乎屏幕空白；
 */
+ (UIImage *)zx_getContextImageFromView: (UIView *)view
{
    // draw a view's contents into an image context
    UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}


+ (BOOL)zx_checkIsValidJPGImageByImageData:(nullable NSData *)imageData
{
    UIImage *image = [UIImage imageWithData:imageData];
    if (!imageData || !image)
    {
        return NO;
    }
    NSData *jpgData = UIImageJPEGRepresentation(image, 1);
    NSData *pngData = UIImagePNGRepresentation(image);
    if (!jpgData && !pngData)
    {
        return NO;
    }
    return YES;
}


- (void)zx_saveImageWithName:(NSString *)imageName
{
    NSData *imageData;
    if (self.size.width>800)
    {
        imageData = UIImageJPEGRepresentation(self, 0.1);//压缩
    }
    else
    {
        imageData = UIImageJPEGRepresentation(self, 1);
    }
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [paths objectAtIndex:0];
    NSString *fullPath = [filePath stringByAppendingPathComponent:imageName];
    NSError *__autoreleasing error = nil;
    if(![imageData writeToFile:fullPath options:NSDataWritingAtomic error:&error])
    {
    }
    else
    {
        [imageData writeToFile:fullPath atomically:NO];
    }
}

+ (UIImage *)zx_imageRotatedWithInitImage:(UIImage *)image newOrientation:(UIImageOrientation)orientation
{
    
    return [UIImage imageWithCIImage:image.CIImage scale:image.scale orientation:orientation];
}
@end
